package v1

import (
	"github.com/gin-gonic/gin"
	"maya-service/bll"
	"maya-service/model"
	"maya-service/server/web/middleware"
	"maya-service/utils"
	"maya-service/utils/request"
)

var SupervisorWithDrawTrans = &supervisorWithDrawTrans{}

func init() {
	RegisterRouter(SupervisorWithDrawTrans)
}

type supervisorWithDrawTrans struct{}

// Init
func (a *supervisorWithDrawTrans) Init(r *gin.RouterGroup) {
	g := r.Group("/supervisorWithDrawTrans", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/detail", a.find)
	}
}

// create
func (a *supervisorWithDrawTrans) create(c *gin.Context) {
	var (
		in  = &model.SupervisorWithDrawTransCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if in.SupervisorId == "" {
		userId, err := request.GetUserId(c.Request.Context())
		if err != nil {
			c.Error(err)
			return
		}
		in.SupervisorId = userId
	}

	if _, err = bll.SupervisorWithDrawTrans.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	// 扣减余额
	find, err := bll.UserRecord.Find(c.Request.Context(), &model.UserRecordInfoRequest{
		UserId: in.SupervisorId,
	})
	if err != nil {
		return
	}

	// 扣减身上的余额
	leftMoney := utils.Sub(find.UserMoney, in.Delta)
	if leftMoney < 0 {
		utils.ResponseOkWithError(c, "余额不足")
		return
	}

	// 更新数据库
	err = bll.UserRecord.Update(c.Request.Context(), &model.UserRecordUpdateRequest{UserId: &in.SupervisorId, UserMoney: &leftMoney})
	if err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// update
func (a *supervisorWithDrawTrans) update(c *gin.Context) {
	var (
		in  = &model.SupervisorWithDrawTransUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.SupervisorWithDrawTrans.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list
func (a *supervisorWithDrawTrans) list(c *gin.Context) {
	var (
		in  = &model.SupervisorWithDrawTransListRequest{}
		out = &model.SupervisorWithDrawTransListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.SupervisorWithDrawTrans.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	var ids []string
	for i := 0; i < len(out.List); i++ {
		ids = append(ids, out.List[i].SupervisorId)
	}

	listMap, _ := bll.GetUserInfoList(c.Request.Context(), ids)
	for i := 0; i < len(out.List); i++ {
		if v, ok := listMap[out.List[i].SupervisorId]; ok {
			out.List[i].SupervisorName = v.UserName
		}
	}

	utils.ResponseOk(c, out)
}

// list
func (a *supervisorWithDrawTrans) find(c *gin.Context) {
	var (
		in  = &model.SupervisorWithDrawTransInfoRequest{}
		out = &model.SupervisorWithDrawTransInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.SupervisorWithDrawTrans.Find(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// delete
func (a *supervisorWithDrawTrans) delete(c *gin.Context) {
	var (
		in  = &model.SupervisorWithDrawTransDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.SupervisorWithDrawTrans.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
